package com.ossjk.ossjkservlet.system.servlet;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.ossjk.core.base.dao.BaseDao;
import com.ossjk.core.base.servlet.BaseServlet;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.AjaxReturn;
import com.ossjk.core.vo.Page;
import com.ossjk.ossjkservlet.common.Constant;
import com.ossjk.ossjkservlet.common.UUIDUtils;
import com.ossjk.ossjkservlet.system.dao.GoodsDao;
import com.ossjk.ossjkservlet.system.dao.ShoppingcarDao;
import com.ossjk.ossjkservlet.system.dao.UserDao;
import com.ossjk.ossjkservlet.system.dao.UsrDao;
import com.ossjk.ossjkservlet.system.pojo.Goods;
import com.ossjk.ossjkservlet.system.pojo.Shoppingcar;
import com.ossjk.ossjkservlet.system.pojo.User;
import com.ossjk.ossjkservlet.system.pojo.Usr;

@WebServlet("/system/shoppingcar.do")
public class ShoppingcarServlet extends BaseServlet {

	private Logger logger = Logger.getLogger(ShoppingcarServlet.class);

	private ShoppingcarDao shoppingcarDao = new ShoppingcarDao();


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String method = getMethod(req);
			if ("list".equals(method)) {
				list(req, resp);
			} else if ("toInsert".equals(method)) {
				toInsert(req, resp);
			} else if ("insert".equals(method)) {
				insert(req, resp);
			} else if ("toUpdate".equals(method)) {
				toUpdate(req, resp);
			} else if ("update".equals(method)) {
				update(req, resp);
			} else if ("delete".equals(method)) {
				delete(req, resp);
			} else if ("batchDelete".equals(method)) {
				batchDelete(req, resp);
			}
		} catch (Exception e) {
			logger.error("", e);
			// 重定向
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 列表
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		List<Shoppingcar> list = shoppingcarDao.selectAll();

		// select * from usr
		// 通过getParamtoObject获取page对象，page 已经有了pageNum ，pageSize
		Page page = getParamtoObject(Page.class, req);
		String name = req.getParameter("name");

		String sql = "select s.*,g.name as gname,c.name as cname from shoppingcar s LEFT JOIN goods g on s.gid = g.id LEFT JOIN customer c on s.cid=c.id"
				;	
	
		List list2 = new ArrayList();
		if (!CommonUtil.isBlank(name)) {
			sql += " where name like  CONCAT('%',?,'%') ";
			list2.add(name);
			page.getPrams().put("name", name);
		}
		System.out.println(sql);
		// selectByPageCount 返回值类型是page page 里面带有了一些查询的数据的
		// selectByPage 返回值类型是list 只有查询结果集的内容
		page = shoppingcarDao.selectByPageCount(sql, page, list2.toArray());
		req.setAttribute("page", page);
		forward(req, resp, "/WEB-INF/page/system/shoppingcar/list.jsp");
	}

	/**
	 * 去增加
	 * 
	 * @param req
	 * @param resp
	 */
	public void toInsert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			forward(req, resp, "/WEB-INF/page/system/shoppingcar/edit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去更新
	 * 
	 * @param req
	 * @param resp
	 */
	public void toUpdate(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.setAttribute("record", shoppingcarDao.selectById(req.getParameter("id")));
			forward(req, resp, "/WEB-INF/page/system/shoppingcar/edit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 增加
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void insert(HttpServletRequest req, HttpServletResponse resp) {
		try {

			// // getParamtoObject
			// // 假设表单和实体类字段一致，可以调用getParamtoObject方法把表单内容自动封装到实体类当中
			Shoppingcar shoppingcar = getParamtoObject(Shoppingcar.class, req);
			//goods.setCrtm(CommonUtil.getDate());
			//goods.setMdtm(CommonUtil.getDate());
			shoppingcar.setId(UUIDUtils.generateUUID());
			if (shoppingcarDao.insert(shoppingcar)> 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 更新
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Shoppingcar shoppingcar = getParamtoObject(Shoppingcar.class, req);
			//goods.setMdtm(CommonUtil.getDate());
			 if (shoppingcarDao.updateBySelected(shoppingcar) > 0) {
			 renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS,
			 Constant.RETURN_MESSAGE_SUCCESS));
			 } else {
			 renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR,
			 Constant.RETURN_MESSAGE_ERROR));
			 }
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void delete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (shoppingcarDao.delete(req.getParameter("id")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void batchDelete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (shoppingcarDao.batchDelete(req.getParameterValues("ids")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

}
